package com.wy.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.project.entity.*;
import com.wy.project.mapper.ProductInfoMapper;
import com.wy.project.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  产品基本信息服务
 * </p>
 *
 * @author Apollo
 * @since 2021-01-16
 */
@Service
public class ProductInfoService extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

    //产品信息
    @Resource
    IProductInfoService productInfoService;
    //历史产品信息
    @Resource
    IHisProductInfoService hisProductInfoService;
    //产品审核信息
    @Resource
    IProductApprovalInfoService productApprovalInfoService;
    //历史产品审核信息
    @Resource
    IHisProductApprovalInfoService hisProductApprovalInfoService;
    //产品终端类型
    @Resource
    IProductInfoTerminalTypeService productInfoTerminalTypeService;
    //历史产品终端类型
    @Resource
    IHisProductInfoTerminalTypeService hisProductInfoTerminalTypeService;
    //产品消息
    @Resource
    IProductMsgService productMsgService;


    /**
     * 产品信息存为草稿(创建)
     *
     * @param productInfo 要保存的信息
     */
    @Override
    public void save4Draft(ProductInfo productInfo) {
        //保存正常基本信息
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        productInfo.setIsDelete(2);
        productInfo.setStatus(ProductInfo.Status.STATUS_DRAFT);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_NOT_ADD);
        productInfo.setPdInfoReqsStatus(ProductInfo.Status.STATUS_NOT_ADD);
        productInfoService.save(productInfo);
        //保存终端类型
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for(ProductInfoTerminalType productInfoTerminalType: productInfoTerminalTypes){
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
    }


    /**
     * 提交审核(创建)
     *
     * @param productInfo 要提交审核的信息
     */
    @Override
    public void save4Approval(ProductInfo productInfo){
        //1:保存基本信息
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        productInfo.setIsDelete(2);
        productInfo.setVersion(1);
        productInfo.setStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_NOT_ADD);
        productInfo.setPdInfoReqsStatus(ProductInfo.Status.STATUS_NOT_ADD);
        productInfoService.save(productInfo);
        //保存终端类型
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
        //2:保存历史基本信息
        HisProductInfo hisProductInfo = new HisProductInfo();
        BeanUtils.copyProperties(productInfo,hisProductInfo);
        hisProductInfo.setCreateTime(new Date());
        hisProductInfo.setUpdateTime(new Date());
        hisProductInfoService.save(hisProductInfo);
        //保存历史终端类型
        List<HisProductInfoTerminalType> hisProductInfoTerminalTypes = new ArrayList<HisProductInfoTerminalType>();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            HisProductInfoTerminalType hisProductInfoTerminalType = new HisProductInfoTerminalType();
            BeanUtils.copyProperties(productInfoTerminalType,hisProductInfoTerminalType);
            hisProductInfoTerminalType.setHisProductInfoCode(hisProductInfo.getCode());
            hisProductInfoTerminalTypes.add(hisProductInfoTerminalType);
        }
        hisProductInfoTerminalTypeService.saveBatch(hisProductInfoTerminalTypes);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(1);//基本信息
        productMsg.setType(1);//待审核
        String title=productInfo.getName()+"的产品基本信息待审核信息";
        productMsg.setTitle(title);
        String content="领导,您有一个产品需要审核,大致如下:"+"产品名称"+productInfo.getName()+"请您审核";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(hisProductInfo.getCode());
        productMsgService.save(productMsg);
    }



    /**
     * 存为草稿(修改)
     *
     * @param productInfo 要修改的信息
     */
    @Override
    public void edit4Draft(ProductInfo productInfo) {
        //修改基本信息
        productInfo.setUpdateTime(new Date());
        productInfo.setId((int)(Math.random()*10000));
        productInfoService.updateById(productInfo);
        //修改终端类型
        QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
        productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,productInfo.getCode());
        productInfoTerminalTypeService.remove(productInfoTerminalTypeQueryWrapper);
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
    }



    /**
     * 提交审核(修改)
     *
     * @param productInfo 要提交审核的信息
     */
    @Override
    public void edit4Approval(ProductInfo productInfo) {
        //1:修改基本信息
        productInfo.setUpdateTime(new Date());
        productInfo.setVersion(1);
        productInfo.setStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        productInfoService.updateById(productInfo);
        //修改终端类型
        QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
        productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,productInfo.getCode());
        productInfoTerminalTypeService.remove(productInfoTerminalTypeQueryWrapper);
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
        //2:保存历史基本信息
        HisProductInfo hisProductInfo = new HisProductInfo();
        BeanUtils.copyProperties(productInfo,hisProductInfo);
        hisProductInfoService.save(hisProductInfo);
        //保存历史终端类型
        ArrayList<HisProductInfoTerminalType> hisProductInfoTerminalTypes = new ArrayList<>();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            HisProductInfoTerminalType hisProductInfoTerminalType = new HisProductInfoTerminalType();
            BeanUtils.copyProperties(productInfoTerminalType,hisProductInfoTerminalType);
            hisProductInfoTerminalType.setHisProductInfoCode(hisProductInfo.getCode());
            hisProductInfoTerminalTypes.add(hisProductInfoTerminalType);
        }
        hisProductInfoTerminalTypeService.saveBatch(hisProductInfoTerminalTypes);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(1);//基本信息
        productMsg.setType(1);//待审核
        String title=productInfo.getName()+"的产品基本信息待审核信息";
        productMsg.setTitle(title);
        String content="领导,您有一个产品需要审核,大致如下:"+"产品名称"+productInfo.getName()+"请您审核";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(hisProductInfo.getCode());
        productMsgService.save(productMsg);
    }





    /**
     * 根据code查询最新版本的信息
     * @param productInfoCode
     * @return
     */
    private Integer findLastVersion(String productInfoCode) {
        QueryWrapper<HisProductInfo> hisProductInfoQueryWrapper = new QueryWrapper<>();
        hisProductInfoQueryWrapper.lambda().eq(HisProductInfo::getCode,productInfoCode).orderByDesc(HisProductInfo::getVersion);
        HisProductInfo hisProductInfo = hisProductInfoService.getOne(hisProductInfoQueryWrapper);
        Integer version = hisProductInfo.getVersion();
        return version;

    }


    /**
     * 审核通过
     *
     * @param productApprovalInfo 审核记录
     */
    @Override
    public void pass(ProductApprovalInfo productApprovalInfo) {
        //1:保存正常基本审核信息
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setPass(1);
        productApprovalInfo.setType(ProductApprovalInfo.Constants.TYPE_BASE);
        productApprovalInfoService.save(productApprovalInfo);
        //修改基本信息状态
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productInfoCode);
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setStatus(ProductInfo.Status.STATUS_APPROVAL_PASS);
        productInfoService.updateById(productInfo);
        //2:保存历史审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        Integer version = productInfo.getVersion();
        hisProductApprovalInfo.setVersion(version);
        hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(1);//基本信息
        productMsg.setType(2);//审核通过
        String title=productInfo.getName()+"的产品基本信息审核成功";
        productMsg.setTitle(title);
        String content="用户您好,你的"+productInfo.getName()+"基本信息审核通过,请您查看";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
        //4:发送待发布消息
        Integer bizFlowStatus = productInfo.getBizFlowStatus();
        Integer pdInfoReqsStatus = productInfo.getPdInfoReqsStatus();
        if(ProductInfo.Status.STATUS_CHANGE_PASS==bizFlowStatus && ProductInfo.Status.STATUS_CHANGE_PASS==pdInfoReqsStatus){
            ProductMsg productMsg1 = new ProductMsg();
            productMsg1.setSendTime(new Date());//发送时间
            productMsg1.setIsRead(2);//未读
            productMsg1.setHandle(2);//未处理
            productMsg1.setKind(1);//基本信息
            productMsg1.setType(5);//审核通过
            String title1=productInfo.getName()+"的产品待发布";
            productMsg.setTitle(title1);
            String content1="用户您好,你的"+productInfo.getName()+"全部审核通过,可以进行发布了";
            productMsg.setContent(content1);
            productMsg.setHisProductInfoCode(productInfo.getCode());
            productMsgService.save(productMsg1);
        }

    }


    /**
     * 审核不通过
     *
     * @param productApprovalInfo 审核记录
     */
    @Override
    public void notPass(ProductApprovalInfo productApprovalInfo) {
        //1:保存基本审核信息
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setPass(2);
        productApprovalInfo.setType(ProductApprovalInfo.Constants.TYPE_BASE);
        productApprovalInfoService.save(productApprovalInfo);
        //修改基本信息状态
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productInfoCode);
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setStatus(ProductInfo.Status.STATUS_APPROVAL_FAIL);
        productInfoService.updateById(productInfo);
        //2:保存历史审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        //维护版本号
        Integer version = productInfo.getVersion();
        hisProductApprovalInfo.setVersion(version);
        hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(1);//基本信息
        productMsg.setType(3);//审核失败
        String title=productInfo.getName()+"的产品基本信息审核失败";
        productMsg.setTitle(title);
        String content="用户您好,你的"+productInfo.getName()+"基本信息审核失败,请您操作";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
    }


    /**
     * 修订
     * 审核失败之后,进行信息的修改,之后提交审核
     *
     * @param productInfo 要审核的信息
     */
    @Override
    public void revise (ProductInfo productInfo) {
        //1:修改基本信息
        productInfo.setUpdateTime(new Date());
        productInfo.setStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        productInfoService.updateById(productInfo);
        //修改终端类型
        QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
        productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,productInfo.getCode());
        productInfoTerminalTypeService.remove(productInfoTerminalTypeQueryWrapper);
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
        //2:保存历史基本信息
        HisProductInfo hisProductInfo = new HisProductInfo();
        BeanUtils.copyProperties(productInfo,hisProductInfo);
        hisProductInfo.setId((int)(Math.random()*10000));
        hisProductInfo.setCreateTime(new Date());
        hisProductInfo.setUpdateTime(new Date());
        //维护版本号,查看当前最新的版本号,然后版本号+1
        //Integer lastVersion = findLastVersion(productInfo.getCode());
        Integer version = productInfo.getVersion();
        hisProductInfo.setVersion(version+1);
        hisProductInfoService.save(hisProductInfo);
        //保存历史终端类型
        ArrayList<HisProductInfoTerminalType> hisProductInfoTerminalTypes = new ArrayList<>();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            HisProductInfoTerminalType hisProductInfoTerminalType = new HisProductInfoTerminalType();
            BeanUtils.copyProperties(productInfoTerminalType,hisProductInfoTerminalType);
            hisProductInfoTerminalType.setHisProductInfoCode(hisProductInfo.getCode());
            hisProductInfoTerminalTypes.add(hisProductInfoTerminalType);
        }
        hisProductInfoTerminalTypeService.saveBatch(hisProductInfoTerminalTypes);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(1);//基本信息
        productMsg.setType(1);//待审核
        String title=productInfo.getName()+"的产品基本信息待审核信息";
        productMsg.setTitle(title);
        String content="领导,您有一个产品需要审核,大致如下:"+"产品名称"+productInfo.getName()+"请您审核";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(hisProductInfo.getCode());
        productMsgService.save(productMsg);
    }

    /**
     * 发布
     *
     * @param code 要发布的标识
     */
    @Override
    public void publish(String code) {
        //1:修改基本信息状态
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setUpdateTime(new Date());
        productInfo.setStatus(ProductInfo.Status.STATUS_PUBLISH);
        productInfoService.updateById(productInfo);
        //2:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(1);//基本信息
        productMsg.setType(6);//发布消息
        String title=productInfo.getName()+"已经发布";
        productMsg.setTitle(title);
        String content="用户您好,你的"+productInfo.getName()+"已经发布,请你查看!";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);

    }


    /**
     * 作废
     *
     * @param code 要作废的信息标识
     */
    @Override
    public void abort(String code) {
        //1:修改基本信息状态
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setUpdateTime(new Date());
        productInfo.setStatus(ProductInfo.Status.STATUS_ABORT);
        productInfoService.updateById(productInfo);
        //2:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(1);//基本信息
        productMsg.setType(4);//作废
        String title=productInfo.getName()+"已经作废";
        productMsg.setTitle(title);
        String content="用户您好,你的"+productInfo.getName()+"已经作废,请你查看!";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
    }


    /**
     * 分页查询
     *
     * @param page 分页的参数
     * @return 分页信息
     */
    @Override
    public Page<ProductInfo> findPage(Page<ProductInfo> page) {
        //产品信息的分页方法
        page = productInfoService.page(page);
        List<ProductInfo> productInfos = page.getRecords();
        for(ProductInfo productInfo : productInfos){
            String code = productInfo.getCode();
            QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
            productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,code);
            List<ProductInfoTerminalType> productInfoTerminalTypes = productInfoTerminalTypeService.list(productInfoTerminalTypeQueryWrapper);
            productInfo.setProductInfoTerminalTypes(productInfoTerminalTypes);
        }
        return page;
    }


    /**
     * 分页查询
     *
     * @param current 每页多少条
     * @param size    当前页
     * @return 分页信息
     */
    @Override
    public Page<ProductInfo> findPage(Integer current, Integer size) {
        Page<ProductInfo> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        return findPage(page);
    }



    /**
     * 查看详情
     *
     * @param code 要查看的标识
     * @return 详情信息
     */
    @Override
    public ProductInfo detail(String code) {
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        return productInfo;
    }

    @Override
    public List<ProductInfoTerminalType> findProductInfoTerminalType(String code) {
        QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
        productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,code);
        List<ProductInfoTerminalType> productInfoTerminalTypeList = productInfoTerminalTypeService.list(productInfoTerminalTypeQueryWrapper);
        return productInfoTerminalTypeList;
    }

    @Override
    public List<ProductMsg> findProductMsg() {
        List<ProductMsg> productMsgList = productMsgService.list();
        return productMsgList;
    }


}
